import { fromJS } from 'immutable';
import {actionType} from './index';
const  stateDefalut  = fromJS({
   ItemList : [],
   dataNote:[],
   articlePage:1,
   users:[],
   usersPage:1,
   allUsersPage:0,
   tip:false
});

const addDataNote = (state,action) =>{
    return state.merge({
        "dataNote":state.get('dataNote').concat(action.data),
        "articlePage":action.nextPage
    })
}
const changeFollowUserList = (state,action)=>{
   const newUsers = state.get('users').toJS();
      for(let i=0;i<newUsers.length;i++){
        if(newUsers[i].id == action.id){
            newUsers[i].is_following_user = !newUsers[i].is_following_user;
            return state.set('users',fromJS(newUsers));
         }
      }
      
}

export default (state = stateDefalut,action) =>{
    switch(action.type){
        case actionType.CHNAGE_ITEMLIST:
         return state.merge({
            'ItemList':action.ItemList,
            'users':fromJS(action.users),
            'allUsersPage':action.allUsersPage
         });
        case actionType.CHNAGE_NOTELIST:
           return state.set('dataNote',action.dataNote);
        case actionType.ADD_NOTELIST:
           return addDataNote(state,action);
        case actionType.CAHNGE_USERPAGE:
           return state.set('usersPage',action.page);
        case actionType.CHANGE_FOLLOWUSER:
           return changeFollowUserList(state,action);
        case actionType.CHANGE_SHOWTIP:
           return state.set('tip',action.isShow);  
        default:
         return state;
    }
    
}